<template>
<div>
    <avue-crud ref="crud"
               :page="page"
               :data="dataList"
               :option="tableOption"
               @search-change="searchChange"
               @on-load="getDataList">
      <!-- <template slot="menuLeft">
        <el-button type="danger"
                   @click="deleteHandle()"
                   v-if="isAuth('admin:user:delete')"
                   size="small"
                   :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </template> -->

      <template slot-scope="scope"
                slot="extensionWorker">
        <el-switch
          v-model="scope.row.extensionWorker"
          active-color="#13ce66"
          inactive-color=""
          active-value="0"
          inactive-value="1"
          active-text="启用"
          @change="handleSwitch($event,scope.row.promotionId)">
        </el-switch>          
        <!-- <el-tag v-if="scope.row.status === 0"
                size="small"
                type="danger">禁用</el-tag>
        <el-tag v-else
                size="small">可推广</el-tag> -->
      </template>

      <template slot-scope="scope"
                slot="menu">
        <template v-if="scope.row.status == 1">
          <el-button type="primary"
                    icon="el-icon-edit"
                    size="small"
                    v-if="isAuth('admin:user:update')"
                    @click.stop="updatePromoter(scope.row.promotionId, 2)">审核通过</el-button>
          <el-button type="danger"
                    icon="el-icon-delete"
                    size="small"
                    v-if="isAuth('admin:user:delete')"
                    @click.stop="updatePromoter(scope.row.promotionId, 3 )">审核拒绝</el-button>
        </template>
        <template v-if="scope.row.status == 2">
          <el-button>审核已通过</el-button>
        </template>
        <template v-if="scope.row.status == 3">
          <el-button type="primary"
                    icon="el-icon-edit"
                    size="small"
                    v-if="isAuth('admin:user:update')"
                    @click.stop="updatePromoterAgain(scope.row.promotionId, 2)">再次审核</el-button>
          <el-button type="danger"
                    icon="el-icon-delete"
                    size="small"
                    v-if="isAuth('admin:user:delete')"
                    @click.stop="updatePromoter(scope.row.promotionId, 3 )">审核拒绝</el-button>
        </template>
      </template>
    </avue-crud>
    <!-- 弹窗, 新增 / 修改 -->

</div>
</template>

<script>
import { tableOption } from '@/crud/user/promoterApplication'
import AddOrUpdate from './promoter-update'

export default {
    data() {
      return {
        addOrUpdateVisible: false,
        tableOption: tableOption,
        dataList: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
      }
    },
    components: {
      AddOrUpdate
    },
    created(){
      // this.getDataList()
    },
    methods: {
      getDataList (page) {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/extension/page'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign({
              current:page == null? this.page.currentPage : page.currentPage,
              size:page == null? this.page.pageSize : page.pageSize,
            },this.params)
          )
        }).then(({ data }) => {
          console.log(data.records);
          
          this.dataList = data.records
          this.page.total = data.total
          this.page.currentPage = data.current
          this.dataListLoading = false
        }).catch((err) =>{
        })
      },

      searchChange (params,done) {
        this.params = params
        console.log(params)
        this.getDataList(this.page)
        done()
    },

    updatePromoter(promotionId,status){
      this.$http({
        url: this.$http.adornUrl(`/extension/updateExtension`),
        method: 'post',
        data: this.$http.adornData({
          promotionId ,
          status,
        })
      }).then(({ data }) => {
        this.$message({
          message: '操作成功',
          type: 'success',
          duration: 1500,
        })
        this.getDataList()
      })
    },

    updatePromoterAgain(promotionId,status){
      this.$confirm('您将通过此条推广人员申请','推广员审核',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() =>{
        this.$http({
          url: this.$http.adornUrl(`/extension/updateExtension`),
          method: 'post',
          data: this.$http.adornData({
            promotionId ,
            status,
          })
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
          })
        this.getDataList()

        })        
      }).catch(() =>{

      })

    },

    handleSwitch(value,promotionId){
      this.$http({
        url: this.$http.adornUrl(`/extension/updateExtensionWorker`),
        method: 'post',
        data: this.$http.adornData({
          promotionId ,
          extensionWorker: value,
        })
      }).then(({ data }) => {
        this.$message({
          message: '操作成功',
          type: 'success',
          duration: 1500,
        })
        this.getDataList()
      })
    }

      
    //   selectionChange (val) {
    //     this.dataListSelections = val
    // },
    // 新增 / 修改
    // updatePromoter (row) {
    //   this.addOrUpdateVisible = true
    //   this.$nextTick(() => {
    //     this.$refs.addOrUpdate.init(row)
    //   })
    // },   

    

  },
    
}
</script>

<style scoped>
</style>>


